<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib prefix="sys" uri="/WEB-INF/tld/sys.tld" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>来款 管理</title>
    <style type="text/css">
        .searchArea .fieldBox .label{width: 70px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>

<body class="iframeBody">

<div class="Wrapper">
    <div class="searchArea">
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="fieldBox">
                    <div class="label">资金编号</div>
                    <div class="field">
                        <input name="code" id="code" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">收款账号</div>
                    <div class="field">
                        <input name="acceptCode" id="acceptCode" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">收款户名</div>
                    <div class="field">
                        <input name="acceptName" id="acceptName" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">来款账号</div>
                    <div class="field">
                        <input name="comeCode" id="comeCode" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">来款户名</div>
                    <div class="field">
                        <input name="comeName" id="comeName" type="text" class="ui-textbox" value="">
                    </div>
                </div>

                <div class="fieldBox">
                    <div class="label">来款金额</div>
                    <div class="field">
                        <input ligerui="width: 82" name="amountMin" id="amountMin" type="text" class="ui-textbox" value="">
                        -
                        <input ligerui="width: 82" name="amountMax" id="amountMax" type="text" class="ui-textbox" value="">
                    </div>
                </div>

                <div class="fieldBox col_33" id="isAutoChargeDiv" >
                    <div class="label">是否自动划扣</div>
                    <div class="field">
                        <select name="isAutoCharge"  id="isAutoCharge"  >
                            <option value=" "  selected="selected">-请选择-</option>
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </div>

                <div class="fieldBox">
                    <div class="label">到账日期</div>
                    <div class="field">
                        <input ligerui="width: 82" name="acceptDateMin" id="acceptDateMin" type="text" class="ui-datepicker" value="">
                        -
                        <input ligerui="width: 82" name="acceptDateMax" id="acceptDateMax" type="text" class="ui-datepicker" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">创建日期</div>
                    <div class="field">
                        <input ligerui="width: 82" name="crtTmMin" id="crtTmMin" type="text" class="ui-datepicker" value="">
                        -
                        <input ligerui="width: 82" name="crtTmMax" id="crtTmMax" type="text" class="ui-datepicker" value="">
                    </div>
                </div>
                <div class="clearFix"></div>
                <div class="formBtns notRight">
                    <span id="searchBtn" class="sq-button sq-button-round"></i>查询</span>
                    <span id="clearBtn" class="sq-button sq-button-round"></i>清空</span>
                    <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                </div>
                <sys:funcAuth resCd="fundModify">
                    <div class="fieldBox">
                        <div class="field">
                            <jsp:include page="../../system/sysUlFilesRec/sysSysUlFilesRecUpload.jsp" flush="true" >
                                <jsp:param name="fileNum" value="1"/>
                                <jsp:param name="filePath" value="capital/capFund"/>
                            </jsp:include>
                        </div>
                        <div class="field">
                            <span id="addBatchBtn" class="sq-button sq-button-round" onclick="addBatch();">批量导入</span>
                            <span id="downTempBtn" class="sq-button sq-button-round" onclick="downTemp();">下载模板</span>
                            <span id="addBtn" class="sq-button sq-button-round"></i>新增来款</span>
                            <span id="modifyBtn" class="sq-button sq-button-round"></i>修改</span>
                            <span id="delBtn" class="sq-button sq-button-round"></i>删除</span>
                        </div>
                    </div>
                </sys:funcAuth>
            </div>
        </form>
    </div>

    <div id="ligergrid1" class="sq-grid"></div>
</div>
<!-- 添加催收记录弹出层 start-->
<div id="divPopUp" style="display:none">
    <form id="divForm">
        <div class="fieldset1 tabel_col tabel_col_s clearFix noBorder">

            <div class="fieldBox col_100">
                <div class="label">可用金额</div>
                <div class="field" id="amtField">
                    8888
                </div>
            </div>


            <div class="fieldBox col_100">
                <div class="label" id="div_rsk_lkman_column_tip">拆分金额</div>
                <div class="field">
                    <input name="divAmt" id="divAmt" validate="{required:true}" type="text" class="ui-textbox" value="">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- 添加催收记录弹出层 end-->
<script type="text/javascript">
    var grid = null;
    var callback = null;
    $(function () {
        grid = $("#ligergrid1").ligerGrid({
            url: '<%=path%>/capital/capFund/initList.do',
            parms:{fundCd:0},
            method: 'get',
            columns: [
                {display: '资金编号', name: 'code', width: 130, align: 'left', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '<a href="javascript:;" style="cursor: pointer;" onclick="qyFund('+rowdata.id+')">'+rowdata.code+'</a>';
                    }
                },
                {display: '收款名称', name: 'acceptName', minWidth: 100, align: 'left', enabledSort: false},
                {display: '收款账号', name: 'acceptCode', minWidth: 80, align: 'left', enabledSort: false},
                {display: '交易(到账)日期', name: 'acceptDate', width: 100, align: 'center', enabledSort: false,render:renderDate},
                {display: '客户名称', name: 'cstNm', width: 90, align: 'left', enabledSort: false,},
                {display: '来款账号', name: 'comeCode', minWidth: 80, align: 'left', enabledSort: false},
                {display: '来款户名', name: 'comeName', width: 100, align: 'left', enabledSort: false},
                {display: '是否自动划扣', name: 'isAutoCharge', minWidth: 100, enabledSort: true,
                    render: function (rowdata, rowindex, value, column) {
                        if (rowdata.isAutoCharge == 1) {
                            return "是";
                        } else if (rowdata.isAutoCharge == 0) {
                            return "否";
                        }
                    }
                },
                {display: '来款金额', name: 'amount', width: 90, align: 'right', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.amount/100);
                    }
                },
                {display: '可用金额', name: 'availableAmount', width: 90, align: 'right', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.availableAmount/100);
                    }
                },
                {display: '创建日期', name: 'crtTm', width: 130, align: 'center', enabledSort: false,render:renderDate2},
                {display: '摘要', name: 'docketRem', minWidth: 100, align: 'center', enabledSort: false},
                {display: '操作', width: 120, align: 'center', enabledSort: false,
                    render:function(rowdata,index,value){
                        // if(!rowdata.usedAmount||rowdata.usedAmount==0){//可修改+废除
                           // return "<a class='sq-button-hasicon' href='javascript:;' onclick='updateFund("+rowdata.id+")'><i class='sqBtnIco_4'></i>修改</a> " +
                           //         "<a class='sq-button-hasicon' href='javascript:;' onclick='delFund("+rowdata.id+")'><i class='sqBtnIco_7'></i>删除</a>";
                        // }
                        // 资金状态为9-已冻结，则不显示资金拆分按钮
                        if(rowdata.fundStatusCd == 9){
                            return '';
                        }
                        // 资金拆分权限
                        <sys:funcAuth resCd="fundDiv">
                            return "<a class='sq-button-hasicon' href='javascript:;' onclick='capDivBtn("+rowdata.id+","+ rowdata.availableAmount+")'><i class='sqBtnIco_4'></i>资金拆分</a> ";
                        </sys:funcAuth>
                        return "";
                    }
                },
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: true,
            detail: {
                height: 'auto',
                onShowDetail: showChildren
            },
            frozen:false
        });
        grid.setHeight(100);

        $("#searchBar").ligerForm();

        $("#pageloading").hide();

        /* 查询 */
        $("#searchBtn").click(function (event) {
            var form = liger.get("searchBar");
            var data = form.getData();
            if(data.crtTmMin==''){
                $.ligerDialog.SQalert({content: "请选择创建开始日期", SQ_type: "warning"});
                return;
            }
            if(data.crtTmMax==''){
                $.ligerDialog.SQalert({content: "请选择创建结束日期", SQ_type: "warning"});
                return;
            }
            var day1 = new Date(data.crtTmMax);
            var day2 = new Date(data.crtTmMin);
            //console.log("day1:" + day1);
            //console.log("day2:" + day2);
            var differDay = Math.abs(day1-day2)/1000/60/60/24;
            //console.log(differDay);
            if(differDay>30){
                $.ligerDialog.SQalert({content: "只能查询30天以内的数据", SQ_type: "warning"});
                return;
            }
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            grid.reload();
        });

        /* 清空 */
        $("#clearBtn").click(function (event) {
            $("#resetBtn").click();  //重置 input-text
            var form = liger.get("searchBar");
            form.setData({  //重置select等特殊表单
                "*Cd": " "
            });
        });
        
        callback = function () {
            grid.reload();
        };

        /* 新增 */
        $("#addBtn").click(function (event) {
            $.ligerDialog.SQopen({
                width: 1150,
                height: 550,
                title: '新增来款',
                url:'<%=path%>/capital/capFund/toAddPage.do'
            });
        });

        //修改
        $("#modifyBtn").click(function (event) {
            var selectedRows = grid.getSelectedRows();
            if (selectedRows.length != 1) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
                return;
            }
            // 冻结状态的资金不支持修改
            if(selectedRows[0].fundStatusCd == 9){
                $.ligerDialog.SQalert({content: LogUtils.LOG_CAP_FUND_NOT_EDIT, SQ_type: "warning"});
                return;
            }
            var _id = selectedRows[0].id;
            $.ligerDialog.open({
                width: 1150,
                height: 550,
                title: '资金修改',
                url:'<%=path%>/capital/capFund/toModifyPage.do?id='+_id
            });

            /*if(!selectedRows[0].usedAmount||selectedRows[0].usedAmount==0) {//可修改+废除
                $.ligerDialog.open({
                    width: 1150,
                    height: 550,
                    title: '资金修改',
                    url:'<%=path%>/capital/capFund/toModifyPage.do?id='+_id
                });
            }else{
                $.ligerDialog.SQalert({content: "资金不可修改", SQ_type: "warning"});
            }*/
        });

        //删除
        $("#delBtn").click(function (event) {
            var selectedRows = grid.getSelectedRows();
            if (selectedRows.length != 1) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
                return;
            }
            if(!selectedRows[0].usedAmount||selectedRows[0].usedAmount==0) {//可修改+废除
                var _ids = [];
                _ids.push(selectedRows[0].id);
                $.ligerDialog.confirm("是否确认删除该笔资金?", function (flag) {
                    if (flag == false)return;
                    var wait_dialog;
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: {ids: JSON.stringify(_ids)},
                        url: '<%=path%>/capital/capFund/removeByIds.do',
                        beforeSend: function () {
                            wait_dialog = $.ligerDialog.SQalert({
                                content: LogUtils.LOG_CMM_DATA_LOADING,
                                SQ_type: "waitting"
                            });
                        },
                        success: function (msg) {
                            wait_dialog.close();
                            if (msg.success == 1) {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                                grid.reload();
                            } else {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                            }
                        }
                    });
                });
            }else{
                $.ligerDialog.SQalert({content: "资金不可删除", SQ_type: "warning"});
            }
        });

        $("#file1").attr("accept","application/vnd.ms-excel");
    });

    /**
     * 展开List
     * @param row
     * @param detailPanel
     * @param callback
     */
    function showChildren(row, detailPanel, callback) {
        var gridBox = document.createElement('div');
        $(gridBox).css('margin', 10);
        $(detailPanel).css({
            'padding': 10,
            'box-sizing': 'border-box'
        }).append(gridBox);
        detailGrid = $(gridBox).ligerGrid({
            url: '<%=path%>/capital/capFund/div/initList.do?id=' + row.id,
            method: 'get',
            columns: [
                { display: '资金编号', name: 'code', width: 80, align: 'left'},
                {
                    display: '可用金额', name: 'availableAmount', width: 90, align: 'right', enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '&yen;' + FormatMoney(rowdata.availableAmount / 100);
                    }
                },
                { display: '拆分时间', name: 'crtTm', width: 150, enabledSort:true,render:renderDate},
                { display: '操作人', name: 'crtUsrNm', minWidth: 150 , enabledSort:true},
                { display: '操作', name: '', minWidth: 150 , enabledSort:true,render:function(rowdata){
                    if(rowdata.usedAmount==0){
                        if(rowdata.usedAmount==0){
                            return "<a class='sq-button-hasicon' href='javascript:;' onclick='delDiv("+rowdata.id+")'><i class='sqBtnIco_4'></i>删除</a> ";
                        }else{
                            return "";
                        }
                    }

                }},
            ],
            root: 'rows',
            hideLoadButton: true,
            width: '100%',
            height: 'auto',
            isScroll: false,
            rownumbers: true,  //显示序号
            checkbox: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            onAfterShowData: callback,
            frozen: false,
            usePager: false
        });
    }
    //删除拆分后的资金
    function delDiv(id){
        var url = "<%=path%>/capital/capFund/div/delDiv.do";
        $.get(url,{id:id},function(res){
            res = JSON.parse(res);
            if(res.success=="1"){
                UIUtils.alertSuccess(res.info);
                setTimeout("grid.reload()",500);
            }else{
                UIUtils.alertError(res.info);
            }
        });
    }
    var divId="";
    // 资金拆分按钮事件
    function capDivBtn(id,amt) {
        $("#amtField").html(amt/100);
        divId = id;
        $.ligerDialog.SQopen({
            width: 800,
            top: 100,
            buttons: [
                {
                    text: "保存", cls: 'l-dialog-btn-ok', onclick:function(){
                        capDivSave();//执行保存
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(){
                        $.ligerDialog.hide();
                    }
                }
            ],
            title : '资金拆分',
            target: $("#divPopUp")
        });
     }
     // 资金拆分dialog-保存按钮
     function capDivSave(){
         var url = "<%=path%>/capital/capFund/div/add.do";
         var amt = $("#divAmt").val();
         $.get(url,{id:divId,amt:amt},function(res){
             res = JSON.parse(res)
             if(res.success=="0"){
                 UIUtils.alertError(res.info);
             }else if(res.success=="1"){
                 UIUtils.alertSuccess(res.info);
                 setTimeout("dialogCallback()",500);

             }
         })
     }
     function dialogCallback(){
         $.ligerDialog.hide();
         grid.reload();
     }
    //查询资金信息
    function qyFund(_id) {
        var onCancel = function (item, Dialog) {
            Dialog.hide();
        };
        $.ligerDialog.SQopen({
            width: 800,
            //height: 300,
            autoFixHeight: true,
            title : '资金信息',
            url: "<%=path%>/capital/capFund/toFundDtlPage.do?id="+_id,
            buttons: [
                {
                    text: "关闭", onclick: onCancel, type: 'cancel'
                }
            ]
        });
    }



    //批量导入
    function addBatch(){
        //获取上传文件地址
        var files=getUploadResultParams();
        //var files=[{filePath:'C:/UPLOADTEST/capital/capFund/1472614427515.xls'}];
        if(files&&files.length>0){
            var filePath=files[0].filePath;
            var fileExtension = filePath.substring(filePath.lastIndexOf('.') + 1);
            if(fileExtension!='xls'&&fileExtension!='XLS'){
                $.ligerDialog.SQalert({content: "请上传.xls后缀的资金文件", SQ_type: "warn"});
                return;
            }
            $.ligerDialog.open({
                cls: 'noBorder_Dialog',
                width: 1000,
                height: 500,
                title: '资金导入数据',
                url: '<%=path%>/capital/capFund/toFundImportPage.do?filePath=' +files[0].filePath
            });
        }else{
            $.ligerDialog.SQalert({content: "请先上传资金文件", SQ_type: "warn"});
        }
    }

    //下载模板
    function downTemp(){
        window.location.href="<%=path%>/resource/template/capital/fundTemp.xls";
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    //格式化时间
    function renderDate2(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }

</script>

</body>
</html>
